package com.carworld.controller;

import java.io.UnsupportedEncodingException;

import com.alibaba.fastjson.JSONObject;
import com.carworld.config.BaseController;
import com.carworld.config.UserInter;
import com.carworld.model.SysUser;
import com.carworld.utils.DicUtil;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.ehcache.CacheKit;

public class UserController extends BaseController {


	public  void  careMood(){
		render("/pages/page_Qrmood.html");
	}
	/**
	 * 根据用户角色进入不同user.html
	 */
	public void userPage() {
		SysUser user = SysUser.dao.findByIdInCache(getUserCookie());
		if (user != null && user.getUserRole() != null) {
			if ("1".equals(user.getUserRole())) {
				System.out.println("用户是股东");
				render("/index/user_role1.html");
			} else if ("2".equals(user.getUserRole())) {
				System.out.println("用户是店长");
				render("/index/user_role2.html");
			} else if ("3".equals(user.getUserRole())) {
				System.out.println("用户是店员");
				render("/index/user_role3.html");
			}else if("4".equals(user.getUserRole())){
				System.out.println("用户是青铜会员");
				render("/index/user_role4.html");
			}else{
				System.out.println("用户是普通顾客");
				render("/index/user_role0.html");
			}
		} else {
			System.out.println("用户是普通顾客");
			render("/index/user_role0.html");
		}
	}

	/**
	 * 个人中心查询会员、会员卡信息
	 */
	public void getUserInfo() {
		JSONObject jsonObject = new JSONObject();
		String userId = getUserCookie();
		jsonObject.put("user", SysUser.dao.findByIdInCache(userId));
		/*jsonObject.put("card", Card.dao.findCardById(userId));
		jsonObject.put("shop", SysOffice.dao.findShopByUserId(userId));
		jsonObject.put("meal", Card.dao.findMealNum(userId));*/
		renderJson(jsonObject);
	}

	/**
	 * 会员通过用户名、手机号身份验证，验证通过后保存微信信息
	 * @throws UnsupportedEncodingException 
	 */
	public void validation() throws UnsupportedEncodingException {
		
		SysUser user = SysUser.dao.getValiation(getPara("userName"), getPara("mobile"));
		String state;
		if (user != null) {
			state = "success";
			this.removeCookie("cid");
			this.setCookie("cid", user.getId(), 30 * 24 * 60 * 60);
			user.setUserIsVerificated("2"); // 用户状态设置为待验证
			user.update();
		} else {
			state = "failure";
		}
		renderText(state);
	}

	/**
	 * 补充会员信息
	 */
	public void supplyUserInfo() {
		String name = getPara("name");
		String mobile = getPara("mobile");
		String adress = getPara("adress");
		String district = getPara("district");
		String status = "failure";
	
		SysUser user = SysUser.dao.findByIdInCache(getUserCookie());
		if (user != null) {
			user.setName(name);
			user.setMobile(mobile);
			user.setPhone(mobile);
			user.setUserPosition(adress);
			user.setCarBrand(district);
			user.setUserIsVerificated("1");
			user.update();
			status = "success";
			System.out.println("用户信息补充成功：" + user.toString());
		} else {
			System.out.println("更新用户信息失败");
		}
		// 更新缓存
		CacheKit.remove(DicUtil.CACHE_SYSUSER, user.getId());
		renderText(status);
	}

	/*
	 * 推广专员绑定分红人脉
	 */
	@Before(UserInter.class)
	public void bindMember() {
		System.out.println("进入绑定");
		String shareBy = getPara("shareBy");
		System.out.println("shareby的是：" + shareBy);
		String userId = getUserCookie();
		JSONObject jsonObject = new JSONObject();
		if (shareBy.equals(userId)) {
			jsonObject.put("state", "0");
			jsonObject.put("message", "不能邀请自己哦~可以将邀请码发给你的朋友");
		}else {
			jsonObject.put("message", "绑定成功");
			jsonObject = SysUser.dao.bindMember(shareBy, userId);
		}
		setAttr("jsonObject", jsonObject);
		render("/pages/page_bindMember.html");
	}
	
	
}
